package com.phkj.qdh.ui.fragment.instructionsheet

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.OnBackPressedCallback
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import cn.com.cybertech.pdk.utils.GsonUtils
import com.google.gson.reflect.TypeToken
import com.phkj.qdh.R
import com.phkj.qdh.bean.CarKeyBean
import com.phkj.qdh.databinding.FragmentRelevantWarningInformationBinding
import com.phkj.qdh.ui.adapter.DangshirenAdapter
import com.phkj.qdh.utils.launchAndRepeatWithViewLifecycle
import kotlinx.coroutines.flow.collectLatest
import rxhttp.wrapper.utils.GsonUtil
import java.lang.reflect.Type

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * 警情详情
 */
class RelevantWarningInformationFragment : Fragment() {
    private var _binding: FragmentRelevantWarningInformationBinding? = null
    private val binding get() = _binding!!
    private var param1: String? = null
    private var param2: String? = null
    private lateinit var vm: InstructionVM
    private val dangshirenAdapter by lazy { DangshirenAdapter() }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
        requireActivity().onBackPressedDispatcher.addCallback(this,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    parentFragmentManager.popBackStack(
                        "jinqgingdetail", FragmentManager.POP_BACK_STACK_INCLUSIVE
                    )
                }
            })
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        _binding = FragmentRelevantWarningInformationBinding.inflate(inflater, container, false)
        vm = ViewModelProvider(this, getInstructionVMProvider())[InstructionVM::class.java]
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.run {
            tvNo.text = param1
            ivBack.setOnClickListener {
                parentFragmentManager.popBackStack(
                    "jinqgingdetail", FragmentManager.POP_BACK_STACK_INCLUSIVE
                )
            }
            rvDangshiren.run {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = dangshirenAdapter
            }

        }
        param1?.let {
            vm.getJingQingDeatil(it)
        }

        launchAndRepeatWithViewLifecycle {
            vm.jingqingDetail.collectLatest {
                try {
                    val data = GsonUtil.fromJson<ArrayList<Danshiren>>(
                        it[0].partyHumanList, object : TypeToken<ArrayList<Danshiren>>() {}.type
                    )
                    dangshirenAdapter.submitList(data)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                if (it.size > 0) {
                    it.get(0).let { b ->
                        binding.tvName.text = "${b.alarmPersonName}（${b.alarmPersonIdcard}）"
                        binding.tvDate.text = "${b.createTime}"
                        binding.tvContent.text = "${b.fcBizContent ?: ""}"
                        binding.tvdanwei.text = "${b.fcDeptName ?: ""}"
                        binding.tvPhone.text = b.alarmPhone
                        binding.tvAdd.text = b.alarmDetailAddress
                        binding.tvType.text = "${b.fcSortName}/${b.sortName ?: ""}"
                        binding.tvTypePeople.text = b.personType
                        binding.yewu.text = b.fcBizClass
                        binding.jingqingtype.text = b.fcTypeName
                        binding.anjianleixin.text = b.fdSortName
                        binding.anjianleixinname.text = b.fdTypeName
                        binding.anjianleixindaima.text = b.fdTypeCode
//                        binding.hujidi.text = b.fdTypeCode

//                        binding.danshiren.text = b.partyHumanList

                    }
                }
            }
        }
    }

    companion object {
        /**
         */
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            RelevantWarningInformationFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}

class Danshiren(
    val id: Int,
    val receiveId: String,
    val partyName: String,
    val partyIdcard: String,
    val partyPhone: String,
    val comment: String,
    val deleteFlag: String,
    val personLabel: String,
    val createTime: String,
    val createUser: String,
)